<?php

// if(isset($_GET["access_formula"])){
// 	if($_GET["access_formula"] == "Lookup"){
// 			$db = new Database();
// 		    $q_get_table_form_name = "SELECT `id`, `form_table_name` FROM `tb_workspace` WHERE `form_name` LIKE '" . $FormName . "' ";
// 		    // print_r($q_get_table_form_name);
// 		    $this_get_table_form = $db->query($q_get_table_form_name, "row");
// 		    // print_r($this_get_table_form);
// 		    if (!empty($ReturnField)) {
// 		        $ReturnField = "`" . $ReturnField . "`";
// 		    } else if (empty($ReturnField) or trim($ReturnField) == "*") {
// 		        $ReturnField = "*";
// 		    }
// 		    $q_string = "SELECT $ReturnField FROM `" . $this_get_table_form["form_table_name"] . "`";
// 		    if (!empty($FieldName) and !empty($Filter)) {
// 		        $q_string .= " WHERE `$FieldName` LIKE '$Filter'";
// 		    }
// 		    // print_r($q_string);
// 		    $this_record = $db->query($q_string, "array");
// 		    $ReturnField = preg_replace("/`/", "", $ReturnField);
// 		    $collect_rec = array();
// 		    for ($ctr = 0; $ctr < count($this_record); $ctr++) {
// 		        array_push($collect_rec, $this_record[$ctr][$ReturnField]);
// 		    }
// 		    // print_r($collect_rec);
// 		    return join($collect_rec,"<--->");
// 	}
// }

$fields = json_decode($_POST['data'], true);
$return_arr = array();

// print_r($fields);
foreach ($fields as $key) {
    $field_name = $key['FieldName'];
    $field_formula = $key['Formula'];
    $formulaDoc = new Formula($field_formula);
    $formulaDoc->addFormSourceData( array("RequestID" => $key['RequestID']) );
    $return_value = $formulaDoc->evaluate();

    array_push($return_arr, array(
        "FieldName" => $field_name,
        "FieldValue" => $return_value)
    );
}

print_r(json_encode($return_arr));
?>